package com.xym.web.controller;

import java.sql.Date;
import java.util.Map;

import javax.print.attribute.standard.DateTimeAtCompleted;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xym.bean.TaotaoResult;
import com.xym.pojo.Item;
import com.xym.pojo.Order;
import com.xym.pojo.User;
import com.xym.pojo.UserThreadLocal;
import com.xym.web.Service.ItemService;
import com.xym.web.Service.OrderService;
import com.xym.web.Service.UserService;
import com.xym.web.interceptor.LoginInterceptor;

@Controller
@RequestMapping("order")
public class OrderController {

	@Autowired
	private ItemService itemService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private UserService userService;
	/**
	 * 请求路径    /order/${item.id}.html   跳转到order.jsp 页面
	 * 参数 item.id
	 * 同步请求
	 * 返回参数 ${item.id}  ${item.images[0]}  ${item.price / 100 }
	 * 
	 */
	@RequestMapping(value="{itemId}",method=RequestMethod.GET)
	public ModelAndView toOrder(@PathVariable("itemId")Long itemId){
		  ModelAndView view = new ModelAndView("order");
		  try {
			Item item = itemService.queryItem(itemId);
			view.addObject("item", item);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	/**
	 * 提交订单
	 * 请求路径   http://www.xym.com/order/submit.html
	 * 请求方式 post  
	 * 携带参数 paymentType:1
				orderItems[0].itemId:14
				orderItems[0].num:1
				orderItems[0].price:12300
				orderItems[0].totalFee:12300
				orderItems[0].title:小饼干
				orderItems[0].picPath:http://image.xym.com/images/2018/01/02/2018010209370573409108.jpg
				payment:123.00
				orderShipping.receiverName:花和尚
				orderShipping.receiverMobile:15800000000
				orderShipping.receiverState:上海
				orderShipping.receiverCity:上海
				orderShipping.receiverDistrict:闵行区
				orderShipping.receiverAddress:三鲁公路3279号 明浦广场 3号楼 205室 
	 * 返回  result.status  业务状态  result.data 订单id
	 * 
	 */
	@RequestMapping(value="submit",method=RequestMethod.POST)
	/**
	 * 提交订单
	 * 异步请求
	 * 参数是Order对象
	 * 结果： status 订单保存状态 ； data 订单id
	 * 
	 */
	public ResponseEntity<TaotaoResult> submit(@CookieValue(LoginInterceptor.COOKIE_NAME) String token, Order order){
		
		try {
			//得到用户
			//User user = this.userService.queryUserByToken(token);
			User user = UserThreadLocal.get();
			//用户id
			order.setUserId(user.getId());
			//用户昵称
			order.setBuyerNick(user.getUsername());
			TaotaoResult resultMap = this.orderService.submitOrder(order);
			return ResponseEntity.status(HttpStatus.OK).body(resultMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	
	/**
	 * 订单提交成功 ，跳转到成功页面  http://www.xym.com/order/success.html?id=null
	 * 返回   ${date}  ${order.payment}
	 * 
	 */
	@RequestMapping(value="success",method=RequestMethod.GET)
	public ModelAndView  toSuccess(@RequestParam("id")String orderId){
		ModelAndView view = new ModelAndView("success");
		Order order = this.orderService.queryOrderById(orderId);
		view.addObject("order", order);
		view.addObject("date", "2018-09-08 19:00:00");
		return view;
	}
}
